#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ImageProcesser.h"
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initial();
    setSignalMappers();
    //Retrieve Signals from ImageProceeser
    ImageProcesser *im=new ImageProcesser(this);
    connect(im,SIGNAL(sendImg (QImage*)),this,SLOT(showImage(QImage*)));
    connect(im,SIGNAL(sendImg (QImage*)),this,SLOT(showMessage(QImage*)));
    connect(im,SIGNAL(sendError(QString)),this,SLOT(recError(QString)));
    connect(im,SIGNAL(sendPath(QString)),this,SLOT(recPath(QString)));
    connect(im,SIGNAL(sendACK(bool)),this,SLOT(recACK(bool)));
}
void MainWindow::setSignalMappers()
{
    //Send Multiple Signals to SignalMapper
    signalMapper= new QSignalMapper(this);
    connect(ui->RGB,SIGNAL(clicked()),signalMapper,SLOT(map()));
    signalMapper->setMapping(ui->RGB, "RGB");
    connect(ui->Gray,SIGNAL(clicked()),signalMapper,SLOT(map()));
    signalMapper->setMapping(ui->Gray, "GRAY");
    connect(ui->Binary,SIGNAL(clicked()),signalMapper,SLOT(map()));
    signalMapper->setMapping(ui->Binary, "BINARY");
    connect(ui->actionInverseColor, SIGNAL(triggered()), signalMapper, SLOT(map()));
    signalMapper->setMapping(ui->actionInverseColor, "INVERSE");
    connect(ui->actionRedo, SIGNAL(triggered()), signalMapper, SLOT(map()));
    signalMapper->setMapping(ui->actionRedo, "REDO");
    connect(ui->actionUndo, SIGNAL(triggered()), signalMapper, SLOT(map()));
    signalMapper->setMapping(ui->actionUndo, "UNDO");
    connect(ui->actionOpenCamera,SIGNAL(triggered()),signalMapper,SLOT(map()));
    signalMapper->setMapping(ui->actionOpenCamera, "openCamera");
    connect(ui->actionCloseCamera,SIGNAL(triggered()),signalMapper,SLOT(map()));
    signalMapper->setMapping(ui->actionCloseCamera, "closeCamera");
    connect(ui->actionCapture,SIGNAL(triggered()),signalMapper,SLOT(map()));
    signalMapper->setMapping(ui->actionCapture, "capture");
    connect(ui->actionSave, SIGNAL(triggered()), signalMapper, SLOT(map()));
    signalMapper->setMapping(ui->actionSave, "save");
    connect(ui->actionOpen, SIGNAL(triggered()), signalMapper, SLOT(map()));
    signalMapper->setMapping(ui->actionOpen, "OPEN");
    connect(ui->actionSavePath, SIGNAL(triggered()), signalMapper, SLOT(map()));
    signalMapper->setMapping(ui->actionSavePath, "savePath");
}
void MainWindow::initial()
{
    setLED(0);
    ui->Binary->setStyleSheet("border:1px solid black;background-color: rgb(230,230,230)");
    ui->Gray->setStyleSheet("border:1px solid black;background-color: rgb(230,230,230)");
    ui->RGB->setStyleSheet("border:1px solid black;background-color: rgb(230,230,230)");
}

void MainWindow::setLED(int color)
{
    ui->LED->setText("");
    // Shape Settings
    QString min_width = QString("min-width: %1px;").arg(32);
    QString min_height = QString("min-height: %1px;").arg(32);
    QString max_width = QString("max-width: %1px;").arg(32);
    QString max_height = QString("max-height: %1px;").arg(32);
    // Border Settings
    QString border_radius = QString("border-radius: %1px;").arg(16);
    QString border = QString("border:1px solid black;");   // 边框为1px黑色
    // Background color Settings
    QString background = "background-color:";
    switch (color) {
    case 0:
        // Red
        background += "rgb(255,0,0)";
        break;
    case 1:
        // Green
        background += "rgb(0,255,0)";
        break;
    default:
        break;
    }
    QString SheetStyle = min_width + min_height + max_width + max_height + border_radius + border + background;
    ui->LED->setStyleSheet(SheetStyle);
}

void MainWindow::showImage(QImage* Img)
{
    ui->ImgLabel->setPixmap(QPixmap::fromImage(*Img));
    ui->ImgLabel->setAlignment(Qt::AlignCenter);
}

void MainWindow::showMessage(QImage* Img)
{
    ui->WidthValue->setText(QString::number(Img->width()));
    ui->HeightValue->setText(QString::number(Img->height()));
}

void MainWindow::recPath(QString str)
{
    ui->ImagePathValue->setText(str);
}

void MainWindow::recError(QString str)
{
    error=new ErrorWindow(this);
    error->setLabel(str);
    error->show();
}
void MainWindow::recACK(bool ack)
{
    if(ack)
      on_RGB_clicked();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_Binary_clicked()   //change color
{
    ui->Binary->setStyleSheet("border:1px solid black;background-color: rgb(175,238,238)");
    ui->Gray->setStyleSheet("border:1px solid black;background-color: rgb(230,230,230)");
    ui->RGB->setStyleSheet("border:1px solid black;background-color: rgb(230,230,230)");
}

void MainWindow::on_Gray_clicked()
{
    ui->Gray->setStyleSheet("border:1px solid black;background-color: rgb(175,238,238)");
    ui->Binary->setStyleSheet("border:1px solid black;background-color: rgb(230,230,230)");
    ui->RGB->setStyleSheet("border:1px solid black;background-color: rgb(230,230,230)");
}

void MainWindow::on_RGB_clicked()
{
    ui->RGB->setStyleSheet("border:1px solid black;background-color: rgb(175,238,238)");
    ui->Binary->setStyleSheet("border:1px solid black;background-color: rgb(230,230,230)");
    ui->Gray->setStyleSheet("border:1px solid black;background-color: rgb(230,230,230)");
}

void MainWindow::on_actionOpenCamera_triggered()
{
    setLED(1);
}

void MainWindow::on_actionSavePath_triggered()
{
    QString curDir=QDir::currentPath();   //choose file
    QString filename=QFileDialog::getExistingDirectory(this,"choose directory");
    if(filename.isEmpty())
    {
         return;
    }
    cameraPath=filename;
    ui->ImagePathValue->setText(filename);
}

void MainWindow::on_actionOpen_triggered()
{
    QString curDir=QDir::currentPath();   //choose file
    QString filename=QFileDialog::getOpenFileName(this,"select Image",curDir,"Images (*.png *.bmp *.jpg *.tif *.GIF )");
    if(filename.isEmpty())
    {
         return;
    }
    getPath=filename;
    ui->ImagePathValue->setText(filename);
    on_RGB_clicked();
}

void MainWindow::on_actionSave_triggered()
{
    QString curDir=QDir::currentPath(); //choose file
    QString filename=QFileDialog::getSaveFileName(this,"save Image",curDir,"Images (*.png *.bmp *.jpg *.tif *.GIF )");
    if(filename.isEmpty())
    {
         return;
    }
    savePath=filename;
    ui->ImagePathValue->setText(filename);
}

void MainWindow::on_actionInverseColor_triggered()
{
    on_RGB_clicked();
}

void MainWindow::on_actionCloseCamera_triggered()
{
    setLED(0);
}
